
#include "websole.h"

namespace coid {

////////////////////////////////////////////////////////////////////////////////
opcd Websole::accept_connect( net_in const charstr& name, net_in const password& pwd, net_out uints* ref )
{
    account_id acid;
    acid._name = name;
    acid._pwd = pwd;
    opcd e = _cmdin->set_identity(acid);
    if(!e)
    {
        netAddress nat;
        _cmdin->get_server_address_nl(&nat);
		charstr tmp;
        e = _lem.connect( name, pwd, nat.getHost(tmp, true).ptr() );

        if(!e)
            _lem.get_log_upd(ref);
    }
    
    return e;
}

////////////////////////////////////////////////////////////////////////////////
opcd Websole::get_current_pos( net_out uints* ref )
{
    return _lem.get_log_upd( ref );
}

////////////////////////////////////////////////////////////////////////////////
opcd Websole::read( net_inout uints& ref, net_out dynarray<token>& rows, net_out uint& flg )
{
    _cmdin->request_autodestroy( SESSION_TIMEOUT );
    _lem.read_log( ref, 16, _ale );

    uint n = (uint)_ale.size();
    _afle.need(n);
    rows.need_new(n);

    for( uint i=0; i<n; ++i )
    {
        charstr& ch = _afle[i];
        ch.reset();

        const logstream& le = *_ale[i];

        format_entry( ch, le );
        rows[i] = ch;
    }

    return 0;
}

////////////////////////////////////////////////////////////////////////////////
opcd Websole::command( const charstr& cmd )
{
    return ersNOT_IMPLEMENTED;
}

////////////////////////////////////////////////////////////////////////////////
void Websole::format_entry( charstr& dst, const logstream& le )
{
    static token fmtdat = "<span class=\"dat\">";

    static token fmterr = "<span class=\"err\">";
    static token fmtwrn = "<span class=\"wrn\">";
    static token fmtsys = "<span class=\"sys\">";
    static token fmtdbg = "<span class=\"dbg\">";
    static token fmtmet = "<span class=\"met\">";

    static token fmtend = "</span>";

    char c;
    token fmt = token::empty();

    switch( le._cclass ) {
    case LOGMSG::TYPE_ALERT:    c = 'A';  fmt = fmterr;  break;
    case LOGMSG::TYPE_CRIT:     c = 'C';  fmt = fmterr;  break;
    case LOGMSG::TYPE_ERROR:    c = 'E';  fmt = fmterr;  break;
    case LOGMSG::TYPE_WARNING:  c = 'W';  fmt = fmtwrn;  break;
    case LOGMSG::TYPE_NOTICE:   c = 'n';  break;
    case LOGMSG::TYPE_INFO:     c = 'i';  break;
    // debug
    case LOGMSG::TYPE_DEBUG:    c = 'D';  fmt = fmtdbg;  break;
    case LOGMSG::TYPE_TRACE:    c = 'T';  fmt = fmtdbg;  break;
    // system
    case LOGMSG::TYPE_OPEN:     c = 'o';  fmt = fmtsys;  break;
    case LOGMSG::TYPE_CLOSE:    c = 'c';  fmt = fmtsys;  break;
    case LOGMSG::TYPE_TERM:     c = 't';  fmt = fmtsys;  break;
    case LOGMSG::TYPE_ATTACH:   c = 'a';  fmt = fmtsys;  break;
    case LOGMSG::TYPE_DETACH:   c = 'd';  fmt = fmtsys;  break;

    case LOGMSG::TYPE_METHOD:   c = 'm';  fmt = fmtmet;  break;

    default:                    c = '?';
    }

    dst << fmtdat;
    dst.append_date_local( le._time, charstr::DATE_HHMMSS );
    dst.append(' ');

    //ip nick
    dst << token(le._src,4);
    dst.append(' ');

    //object id
    dst.append_num( 10, le._obj, 4, ALIGN_NUM_RIGHT );
    dst.append(' ');

    //object name
    dst << token::from_cstring( le._short, 8 );
    dst.append(' ');

    //message type
    dst.append(c);
    dst.append(' ');

    dst << fmtend;

    //message text
    tmp.reset();
    dst << fmt;
    dst << check_html( le.text(), tmp );
    if( !fmt.is_empty() )
        dst << fmtend;
}



} //namespace coid
